package com.njlime.cms.service.sys;

import com.njlime.cms.entities.Community;
import com.njlime.cms.repository.sys.CommunityDao;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.sys.service.UnitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by   on 2017/1/12.
 */
@Service
@Transactional(readOnly = true)
public class CommunityService {
    @Resource
    CommunityDao communityDao;
    @Resource
    private UnitService unitService;


    /**
     * 详情
     *
     * @param id
     * @return
     */
    public Community findOne(int id) {
        return communityDao.findOne(id);
    }

    /**
     * 保存
     *
     * @param community
     * @return
     */
    @Transactional(readOnly = false)
    public Integer save(Community community) {
        if (community.getPublish_status() != null && community.getPublish_status().intValue() != GlobalValue.PublishStatus.UNPUBLISHED) {//不是定时发送的，则发送时间为当前时间
            community.setPublish_time(new Date());
        }
        community.setRoot_unit_id(unitService.getRootUnit(community.getUnit_id()));
        community.setComment_num(0);
        community.setReading(0);
        community.setIs_comment(GlobalValue.NO);
        community.setIs_del(GlobalValue.NO);
        community.setCreate_time(new Date());
        communityDao.save(community);
        return community.getId();

    }

    /**
     * 更新
     *
     * @param info
     * @return
     */
    @Transactional(readOnly = false)
    public int update(Community info) {
        return communityDao.editCommunity(info);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int del(int id) {
        return communityDao.del(id);
    }

    /**
     * 评论开关
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int toggleComment(int id) {
        return communityDao.toggleComment(id);
    }

    /**
     * 上下线
     *
     * @param community
     * @return
     */
    @Transactional(readOnly = false)
    public int togglePublish(Community community) {
        if (community.getPublish_status() != GlobalValue.PublishStatus.PUBLISHED) {
            community.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
        } else {
            community.setPublish_status(GlobalValue.PublishStatus.CANCELED);
        }
        return communityDao.editCommunity(community);
    }

    /**
     * 分页列表
     *
     * @param pageBean
     * @param info
     * @return
     */
    public PageBean list(PageBean pageBean, Community info, boolean is_all) {
        return communityDao.list(pageBean, info, is_all);
    }

    /**
     * 某篇文章阅读数量 +1
     *
     * @param id 文章id
     * @return
     */
    @Transactional(readOnly = false)
    public int updateReading(int id) {
        return communityDao.updateReading(id);
    }

    /**
     * 某篇文章评论数量 +1
     *
     * @param id 文章id
     * @return
     */
    @Transactional(readOnly = false)
    public void updateCommentNum(int id, Integer num) {
        communityDao.updateCommentNum(id, num);
    }
}
